import sun.misc.Signal;  
import sun.misc.SignalHandler;

public class SigHandler implements SignalHandler{
	private SignalHandler oldHandler;  
	
	public void handle(Signal signal) {
		System.out.println("Signal handler called for signal " + signal);
		
		try{
            signalAction(signal);  
         
            // Chain back to previous handler, if one exists  
            if (oldHandler != SIG_DFL && oldHandler != SIG_IGN) {  
                oldHandler.handle(signal);  
            }  
            
		}catch(Exception ex)
		{
			System.out.println("Signal handler called failed for signal " + signal+"reason: " + ex.getMessage());
			ex.printStackTrace();
		}
	}

	public void signalAction(Signal signal) {  
	   //Thread.sleep(5000);  
		System.out.println("building interrupted by unexpect events.exit ");
		System.exit(2);      
	}  
	
	public static SignalHandler register(String signalName) {  
        Signal diagSignal = new Signal(signalName);  
        SigHandler instance = new SigHandler();  
        instance.oldHandler = Signal.handle(diagSignal, instance);  
        return instance;  
    }  	
	
}
